package com.wolfalpha.utron.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.adapter.ItemProjectAdapter;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemProject;
import com.wolfalpha.utron.service.ProjectService;
import com.wolfalpha.utron.service.impl.ProjectServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.list.WaterDropListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的全部项目
 * <p/>
 * Created by luodon on 15/9/8.
 */
public class MyProjectsActivity extends BaseNormalActivity implements WaterDropListView.IWaterDropListViewListener {

    private List<ItemProject> projects;

    private WaterDropListView listView;
    private ItemProjectAdapter adapter;

    private ProjectService projectService;

    private int userId = -1;
    private String username = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("userId")) {
            userId = AppContext.getInstance().getUserId();
        } else {
            userId = bundle.getInt("userId");
            username = bundle.getString("userName");
        }

        setContentView(R.layout.activity_my_projects);

        intiView();
        loadData();
    }

    private int page = 1;

    @Override
    public void onRefresh() {
        page = 1;
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                projects = projectService.getMyList(userId, page);
                handler.sendEmptyMessage(LOAD_REFRESH);
            }
        });
    }

    @Override
    public void onLoadMore() {
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                List<ItemProject> projects1 = new ArrayList<ItemProject>();
                projects1 = projectService.getMyList(userId, ++page);
                if (projects1 != null) {
                    for (ItemProject item : projects1) {
                        projects.add(item);
                    }
                    handler.sendEmptyMessage(LOAD_MORE);
                } else {
                    page--;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            listView.setPullLoadEnable(false);
                        }
                    });
                    handler.sendEmptyMessage(LOAD_MORE);
                }

            }
        });
    }


    private final int LOAD_MORE = 1;    //加载更多
    private final int LOAD_REFRESH = 2; //刷新

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case LOAD_MORE:
                    if (projects != null) {
                        adapter.setData(projects);
                    } else {
                        listView.setPullLoadEnable(false);
                    }
                    listView.stopLoadMore();
                    break;
                case LOAD_REFRESH:
                    if (projects != null) adapter.setData(projects);
                    listView.stopRefresh();
                    break;
            }
        }
    };

    /**
     * 加载列表的数据
     */
    private void loadData() {
        projects = new ArrayList<ItemProject>();
        adapter = new ItemProjectAdapter(MyProjectsActivity.this, projects);
        listView.setAdapter(adapter);
        onRefresh();
    }

    /**
     * 初始化界面
     */
    private void intiView() {
        if (userId == AppContext.getInstance().getUserId()) {
            setTvTitle("我的项目");
        } else {
            setTvTitle(username + "的项目");
        }

        projectService = new ProjectServiceImpl();
        listView = (WaterDropListView) findViewById(R.id.wl_project);
        listView.setWaterDropListViewListener(this);
        listView.setPullLoadEnable(false);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1 || position > projects.size()) {
                    //过滤刷新头部
                    return;
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("pId", projects.get(position - 1).getId());
                    UIHelper.showActivity(MyProjectsActivity.this, ProjectDetailActivity.class, bundle);
                }
            }
        });

    }

}
